package com.fjnldx.mes.service.alarm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fjnldx.mes.domain.domainReq.DeleteReq;
import com.fjnldx.mes.domain.domainReq.Id;
import com.fjnldx.mes.domain.domainReq.IdList;
import com.fjnldx.mes.domain.domainReq.alarm.*;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.domainRes.ResultObject;
import com.fjnldx.mes.domain.domainRes.alarm.AlarmDetailRes;
import com.fjnldx.mes.domain.domainRes.alarm.AlarmPageRes;
import com.fjnldx.mes.domain.pojo.alarm.AlarmConfig;
import com.fjnldx.mes.domain.pojo.alarm.AlarmNotice;
import com.fjnldx.mes.domain.pojo.alarm.AlarmRules;
import com.fjnldx.mes.mapper.alarm.AlarmConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AlarmConfigService {
    @Resource
    private AlarmConfigMapper alarmConfigMapper;

    @Resource
    private AlarmRuleService alarmRuleService;

    @Resource
    private AlarmNoticeService alarmNoticeService;

    /**
     * 获取告警配置集合
     *
     * @return 集合结果
     */
    public Result getPages(QueryAlarmConfigPageReq pageRequest) throws Exception {
        try {
            //条件拼接
            QueryWrapper<AlarmConfig> alarmConfigQueryWrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(pageRequest.getName()) && !"null".equals(pageRequest.getName())) {
                alarmConfigQueryWrapper.like("name", pageRequest.getName());
            }
            //分页查询
            List<AlarmPageRes> page = alarmConfigMapper.GetPage(pageRequest.getName(), pageRequest.getPageNum(), pageRequest.getPageSize());
            Integer total = alarmConfigMapper.GetTotal(pageRequest.getName());

            //封装返回结果
            ResultObject<AlarmPageRes> res = new ResultObject<>();
            res.setDataList(page);
            res.setTotal(total);
            return Result.success(res);
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 获取告警配置详情
     *
     * @param detailRequest 告警配置id
     * @return 告警配置详情
     * @throws Exception 未知异常信息
     */
    public Result getDetail(Id detailRequest) throws Exception {
        try {
            //查询配置信息
            AlarmConfig info = alarmConfigMapper.selectById(detailRequest.getId());
            if (info == null) {
                return Result.error("查询失败!");
            }
            //查询配置规则
            List<AlarmRules> rulesResult = alarmRuleService.getAlarmRules(detailRequest.getId());
            //查询配置通知
            List<AlarmNotice> noticeResult = alarmNoticeService.getAlarmNotice(detailRequest.getId());
            //封装返回结果
            AlarmDetailRes res = new AlarmDetailRes();
            res.setId(info.getId());
            res.setCode(info.getCode());
            res.setName(info.getName());
            res.setIsActive(info.getIsActive());
            res.setNote(info.getNote());
            res.setCreateTime(info.getCreateTime());
            res.setRules(rulesResult);
            res.setNotices(noticeResult);
            return Result.success(res);
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 添加告警配置
     *
     * @param param 告警配置
     * @return 响应信息
     * @throws Exception 未知异常信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Result addConfig(AddAlarmConfigReq param) throws Exception {
        try {
            AlarmConfig alarmConfig = new AlarmConfig();
            alarmConfig.setCode(param.getCode());
            alarmConfig.setName(param.getName());
            alarmConfig.setIsActive(param.getIsActive());
            alarmConfig.setNote(param.getNote());
            alarmConfig.setIsDeleted(false);
            alarmConfig.setCreateTime(LocalDateTime.now());
            int insert = alarmConfigMapper.insert(alarmConfig);
            if (param.getRules() != null) {
                alarmRuleService.UpdateAlarmRule(alarmConfig.getId(), param.getRules());
            }

            if (param.getNotices() != null) {
                alarmNoticeService.UpdateAlarmNotice(alarmConfig.getId(), param.getNotices());
            }
            return insert > 0 ? Result.success("新增成功!") : Result.error("新增失败!");
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 删除告警配置
     *
     * @param param 删除参数
     * @return 响应信息
     * @throws Exception 未知异常信息
     */
    public Result deleteConfig(DeleteReq param) throws Exception {
        try {
            //条件拼接
            QueryWrapper<AlarmConfig> alarmConfigQueryWrapper = new QueryWrapper<>();
            alarmConfigQueryWrapper.in("id", (Object[]) param.getArray());
            int delete = alarmConfigMapper.delete(alarmConfigQueryWrapper);
            return delete > 0 ? Result.success("删除成功!") : Result.error("删除失败!");
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 更新告警配置
     *
     * @param param 告警配置
     * @return 响应信息
     * @throws Exception 未知异常信息
     */
    public Result editConfig(EditAlarmConfigReq param) throws Exception {
        try {
            //查询告警配置
            AlarmConfig configInfo = alarmConfigMapper.selectById(param.getId());
            if (configInfo == null) {
                return Result.error("更新失败，未找到对应的告警配置!");
            }
            //更新数据
            configInfo.setCode(param.getCode());
            configInfo.setName(param.getName());
            configInfo.setIsActive(param.getIsActive());
            configInfo.setNote(param.getNote());
            alarmConfigMapper.updateById(configInfo);
            if (param.getRules() != null) {
                alarmRuleService.UpdateAlarmRule(configInfo.getId(), param.getRules());
            }

            if (param.getNotices() != null) {
                alarmNoticeService.UpdateAlarmNotice(configInfo.getId(), param.getNotices());
            }
            return Result.success("更新成功!");
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 上/下线告警配置
     *
     * @return 响应信息
     * @throws Exception 未知异常信息
     */
    public Result activeConfig(IdList param, Boolean isActive) throws Exception {
        try {
            //条件拼接
            UpdateWrapper<AlarmConfig> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("id", param.getIdList());
            updateWrapper.set("isActive", isActive);
            int delete = alarmConfigMapper.update(null, updateWrapper);
            return delete > 0 ? Result.success("修改成功!") : Result.error("修改成功!");
        } catch (Exception e) {
            throw new Exception();
        }
    }
}
